import 'package:flutter/foundation.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:flutter/material.dart';
import 'package:syria_sports/data/models/auth/user.dart';
import 'package:syria_sports/presentation/admins/add_admin_page/bloc/add_admin_page_screen.dart';

import '../../../../data/controllers/users_controller.dart';
import '../../../../dialogs/delete_player_confirmation.dart';
import '../../../coaches/coach_profile_screen/coach_profile_screen.dart';


class AdminItemView extends StatelessWidget {
  final User user;
  const AdminItemView({super.key, required this.user});
  @override
  Widget build(BuildContext context) {
    dynamic rate = num.tryParse(user.rate??'0.0')??0.0;
    rate = rate.toStringAsFixed(1);
    return Slidable(
      enabled: PrefUtils.isGymAdmin(),
      // The start action pane is the one at the left or the top side.
      startActionPane: ActionPane(
        // A motion is a widget used to control how the pane animates.
        motion: const ScrollMotion(),
        children: [
          // A SlidableAction can have an icon and/or a label.
          SlidableAction(
            onPressed: (context){
              showDeleteConfirmation(context, user);
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.red,
            icon: Icons.delete,
            label: 'حذف',
          ),
          SlidableAction(
            onPressed: (context){
              Navigator.push(context, MaterialPageRoute(builder: (context) => AddAdminPageScreen(coach:user,)));
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.orange,
            icon: Icons.edit,
            label: 'تعديل',
          ),
        ],
      ),
      child: Card(
        color: Colors.white10,
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: InkWell(
            onTap: (){
               Navigator.push(context, MaterialPageRoute(builder: (context) => CoachProfileScreen.builder(context, user)));
            },
            child:SizedBox(
              height: 125,
              child: Row(
                children: [

                  if(user.image == null || user.image!.isEmpty)
                    Container(
                      width: 130,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(16),
                        border: Border.all(color: Colors.redAccent,width: 2),
                        image: DecorationImage(
                            image:Image.asset(ImageConstant.imgLogo).image
                        ),
                      ),
                    )
                  else
                    Container(
                      width: 130,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(16),
                        border: Border.all(color: Colors.redAccent,width: 2),
                        image: DecorationImage(
                            fit: BoxFit.cover,
                            image:Image.network(user.image!).image
                        ),
                      ),
                    ),
                  const SizedBox(width: 8),
                  Expanded(
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text(user.fullName,style: theme.textTheme.titleMedium),
                        Text(user.description??"لا يوجد وصف",style: CustomTextStyles.bodySmallOnPrimary),
                        const SizedBox(height:8),
                        Text("التقييم: $rate%",style: CustomTextStyles.bodySmallOnPrimary),
                      ],
                    ),
                  ),
                ],
              ),
            )
          ),
        ),
      ),
    );
  }

  void showDeleteConfirmation(BuildContext context, User player) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: player.fullName,
          titleUser: 'المدير',
          onConfirm: () {
            UsersController.deleteById(player.userId??'-1');
            if (kDebugMode) {
              print('تم حذف المدير ${player.fullName}!');
            }
          },
        );
      },
    );
  }
}
